﻿@namespace LaserMark.Components
@using System.Diagnostics
@using System.Windows
@using LaserMark.Components.Services
@using LaserMark.View.Services

<div class="form-footer">
    @if (BodyTemplate != null)
    {
        @BodyTemplate
    }
    <PopConfirmButton Icon="fa fa-save" Text="保存" Color="Color.Primary" OnConfirm="OnConfirm" OnClose="OnClose" Placement="Placement.Top">
        <BodyTemplate>
            <div>请输入管理员密码：</div>
            <BootstrapPassword @bind-Value="@Value" class="mt-2"></BootstrapPassword>
        </BodyTemplate>
    </PopConfirmButton>
</div>


@code {

    [Inject]
    [NotNull]
    private SwalService? SwalService { get; set; }

    [Inject]
    [NotNull]
    private ToastService? ToastService { get; set; }

    [Inject]
    [NotNull]
    private IGlobalStatus? GlobalStatus { get; set; }

    private string? Value { get; set; }

    [CascadingParameter]
    [NotNull]
    public Func<Task>? CloseAsync { get; set; }

    [Parameter]
    public RenderFragment? BodyTemplate { get; set; }

    [Parameter]
    public Func<string?, Task<bool>>? OnSave { get; set; }

    private async Task OnConfirm()
    {
        if (OnSave != null)
        {
            var ret = await OnSave(Value);

            if (ret)
            {
                var aa = await SwalService.ShowModal(new SwalOption()
                    {
                        Category = SwalCategory.Warning,
                        Content = "修改配置需从新启动项目"
                    });
                if (aa)
                {
                    GlobalStatus.IsRestart = true;
                    Application.Current.Shutdown();
                }
            }
            else
            {
                await ToastService.Error("提示", "密码输入错误");
            }
        }

        Value = "";
    }

    private Task OnClose()
    {
        Value = "";
        return Task.CompletedTask;
    }

}
